package ui.swing.graphics.clock;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import ui.swing.Console;

public class TextTestFrame extends JFrame {

	JTextField hourField;
	JTextField minuteField;
	private ClockPanel clock;

	public TextTestFrame() {
		setTitle("Text Test");

		DocumentListener listener = new ClockFieldListener();

		JPanel panel = new JPanel();
		panel.add(new JLabel("Hours:"));
		hourField = new JTextField("12", 3);
		panel.add(hourField);
		hourField.getDocument().addDocumentListener(listener);

		panel.add(new JLabel("Minues:"));
		minuteField = new JTextField("00", 3);
		panel.add(minuteField);
		minuteField.getDocument().addDocumentListener(listener);

		add(panel, BorderLayout.SOUTH);

		// add the clock
		clock = new ClockPanel();
		add(clock, BorderLayout.CENTER);
		pack();
	}
	
	public void  setClock() {
		try {
			int hours = Integer.parseInt(hourField.getText().trim());
			int minutes = Integer.parseInt(minuteField.getText().trim());
			clock.setTime(hours, minutes);
		} catch (NumberFormatException e) {
			// TODO
		}
	}

	class ClockFieldListener implements DocumentListener {

		public void changedUpdate(DocumentEvent e) {
			setClock();
		}

		public void insertUpdate(DocumentEvent e) {
			setClock();
		}

		public void removeUpdate(DocumentEvent e) {
			setClock();
		}
	}
	
	public static void main(String[] args) {
		Console.run(new TextTestFrame(), 400, 400);
	}
}
